package com.yuexun.beilunpatient.ui.login.ui;

import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.ui.bean.BaseListEntity;
import com.yuexun.beilunpatient.ui.login.bean.RegisterBean;
import com.yuexun.beilunpatient.ui.login.bean.SmsBean;
import com.yuexun.beilunpatient.ui.login.model.impl.LoginModel;
import com.yuexun.beilunpatient.ui.login.presenter.impl.RegisterPresenter;
import com.yuexun.beilunpatient.ui.login.ui.view.IRegisterView;
import com.yuexun.beilunpatient.ui.main.ui.Act_Webview;
import com.yuexun.beilunpatient.widget.ToastDialog;

import org.kymjs.kjframe.ui.ViewInject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/3/19 0019.
 */

public class Act_Register extends BaseKJActivity implements IRegisterView {

    @Bind(R.id.username_edit)
    EditText usernameEdit;
    @Bind(R.id.username_identity)
    EditText usernameIdentity;
    @Bind(R.id.username_card)
    EditText usernameCard;
    @Bind(R.id.auth_code)
    EditText authCode;
    @Bind(R.id.password_edit)
    EditText passwordEdit;
    @Bind(R.id.isCheck)
    CheckBox isCheck;
    @Bind(R.id.authBt)
    TextView authBt;
    @Bind(R.id.next)
    Button next;

    RegisterPresenter presenter;
    ToastDialog dialog;

    @Override
    public void setRootView() {
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        presenter = new RegisterPresenter(new LoginModel(), this);
    }

    @OnClick({R.id.title_back, R.id.agreement, R.id.next, R.id.authBt, R.id.isCheck, R.id.tv_conceal, R.id.cardHelp})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.agreement:
//                Uri uri = Uri.parse("https://51wangzhen.com/app/app_agreement.html");
//                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
//                startActivity(intent);
                Intent intent = new Intent(aty, Act_Webview.class);
                intent.putExtra("url", "https://51wangzhen.com/app/app_agreement.html");
                intent.putExtra("title", "注册协议");
                startActivity(intent);
                break;
            case R.id.tv_conceal:
//                Uri uri2 = Uri.parse("https://51wangzhen.com/app/beilun_privacy.html");
//                Intent intent2 = new Intent(Intent.ACTION_VIEW, uri2);
//                startActivity(intent2);
                Intent intent2 = new Intent(aty, Act_Webview.class);
                intent2.putExtra("url", "https://51wangzhen.com/app/beilun_privacy.html");
                intent2.putExtra("title", "隐私权政策");
                startActivity(intent2);
                break;
            case R.id.cardHelp:
                Intent intentHelp = new Intent(aty, Act_Webview.class);
                intentHelp.putExtra("url", "https://gzjk.nbwjw.gov.cn/f/txbz");
                intentHelp.putExtra("title", "填写帮助");
                startActivity(intentHelp);
                break;
            case R.id.isCheck:
                if (isCheck.isChecked()) {
                    next.setEnabled(true);
                    next.setBackgroundColor(aty.getResources().getColor(R.color.main_text_02));
                } else {
                    next.setEnabled(false);
                    next.setBackgroundColor(aty.getResources().getColor(R.color.txt_black2));
                }
                break;
            case R.id.next:
                if (usernameEdit.getText().toString().isEmpty()) {
                    ViewInject.toast("请输入姓名");
                    break;
                }
                if (usernameIdentity.getText().toString().isEmpty()) {
                    ViewInject.toast("请输入身份证");
                    break;
                }
                if (usernameCard.getText().toString().isEmpty()) {
                    ViewInject.toast("请输入就诊卡号");
                    break;
                }
                if (!isCard(usernameCard.getText().toString())) {
                    ViewInject.toast("就诊卡号不正确");
                    break;
                }
                if (authCode.getText().toString().isEmpty()) {
                    ViewInject.toast("请输入验证码");
                    break;
                }
                if (passwordEdit.getText().toString().isEmpty()) {
                    ViewInject.toast("请输入密码");
                    break;
                }
                if(checkPwd(passwordEdit.getText().toString())>=2) {
                    if (passwordEdit.getText().toString().length() > 7) {
                        Map<String, String> params = new HashMap<>();
                        params.put("fullName", usernameEdit.getText().toString());
                        params.put("idCardNo", usernameIdentity.getText().toString());
                        params.put("healthNo", usernameCard.getText().toString());
                        params.put("password", passwordEdit.getText().toString());
                        params.put("verifyCode", authCode.getText().toString());
                        params.put("appKey", "Android");
                        presenter.registerOfPatient(params);

                        dialog = new ToastDialog(aty, "正在注册，请稍后");
                        dialog.show();
                    } else {
                        ViewInject.toast("输入字符位数不正确！");
                    }
                }else {
                    ViewInject.toast("请确认含数字、大写字母、小写字母、符号中两种或两种以上");
                }
                break;
            case R.id.authBt:
                if (!usernameEdit.getText().toString().isEmpty() && !usernameIdentity.getText().toString().isEmpty() && !usernameCard.getText().toString().isEmpty()) {
                    Map<String, String> params = new HashMap<>();
                    params.put("fullName", usernameEdit.getText().toString());
                    params.put("idCardNo", usernameIdentity.getText().toString());
                    params.put("healthNo", usernameCard.getText().toString());
                    presenter.registerOfPatientSms(params);
                    /** 倒计时60秒，一次1秒 */
                    CountDownTimer timer = new CountDownTimer(60 * 1000, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            // TODO Auto-generated method stub
                            authBt.setText("还剩" + millisUntilFinished / 1000 + "秒");
                            authBt.setEnabled(false);
                        }

                        @Override
                        public void onFinish() {
                            authBt.setText("获取验证码");
                            authBt.setEnabled(true);
                        }
                    }.start();
                } else {
                    ViewInject.toast("姓名,身份证和医保卡不能为空");
                }
                break;
            default:
                break;
        }
    }

    private int checkPwd(String pwd)
    {
        int i = pwd.matches(".*\\d+.*") ? 1 : 0;
        int j = pwd.matches(".*[a-z]+.*") ? 1 : 0;
        int k = pwd.matches(".*[A-Z]+.*") ? 1 : 0;
        int l = pwd.matches(".*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\"]+.*") ? 1 : 0;

        return  i+j+k+l;
    }

    private boolean isCard(String card){
        if(card.length() == 9){
            return true;
        }
        if(card.length() > 4 && card.substring(0,4).equals("0574")) {
            return true;
        }
        return false;
    }

    @Override
    public void registerOfPatientSms(BaseListEntity<SmsBean> entity) {
        if (entity.getStatus().equals("200")) {
            ViewInject.toast(entity.getDatas().get(0).getDescription());
        }else{
            ViewInject.toast(entity.getMessage());
        }
    }

    @Override
    public void registerOfPatient(BaseListEntity<RegisterBean> entity) {
        dialog.dismiss();
        if (entity.getStatus().equals("200")) {
            if (entity.getDatas().get(0).getCode().equals("1")) {
                finish();
            } else {
                ViewInject.toast(entity.getMessage());
            }
        }else {
            ViewInject.toast(entity.getMessage());
        }
    }

    @Override
    public void fail() {
        dialog.dismiss();
    }

}
